function [result, Ir ] = character_Segmentation1( img, IS_DEBUG, FOLDER_RESULT,file_name )
%CHARACTER_SEGMENTATION Summary of this function goes here
%   Detailed explanation goes here

z = size(img,3);
if z==3
    img = rgb2gray(img);
end
maxwidth = 256;
img=imresize(img,[NaN maxwidth]);

% xmin= 5;
% ymin= 5;
% [maxhigh, maxwidth] = size(img);
% width = maxwidth - 2*xmin;
% high = maxhigh-2*ymin;
% img = imcrop(img, [xmin ymin width high]);

% im1 = padarray(img,[5 5],180,'both');


im1= medfilt2(img,[3 3]); %Median filtering the image to remove noise%
BW = edge(im1,'canny'); %finding edges
% BW = imdilate(BW);
msk=[0 0 0 0 ;
    0 1 1 0 ;
    0 1 1 0 ;
    0 0 0 0 ];
B=conv2(double(BW),double(msk),'same');
im2 = imfill(B,'hole');

SE = ones(1,5);
im2 = imopen(im2,SE);
SE = ones(5,1);
im2 = imopen(im2,SE);
SE = ones(1,5);
im2 = imopen(im2,SE);
im2 = bwareaopen(im2, 200);
Ir = im2;


Ib = Sauvola(img,[0.1 128 15]);
Ib = bwareaopen(~Ib, 100);

h = size(im2,1);
result = {};
resulttemp = {};
numResult = 0;
cc =  bwconncomp(im2, 8);
featureData = regionprops(cc,'BoundingBox');

for i=1:cc.NumObjects
    
    width = featureData(i).BoundingBox(3);
    height = featureData(i).BoundingBox(4);
    
    if(width>5 && width<60 && height>h*0.5 && height<h*0.9)
        subimg1 = imcrop(im2,featureData(i).BoundingBox);
        subimg2 = imcrop(Ib,featureData(i).BoundingBox);
        ftemp = imfill(subimg2,'hole');
        ftemp = xor(ftemp,subimg2);
        
        subimg = xor(subimg1, ftemp);
        subimg = bwareaopen(subimg, 150);
        %         CC1 = bwconncomp(subimg1,8);
        %         fd = regionprops(CC1,'BoundingBox');
        %         numPixels = cellfun(@numel,CC1.PixelIdxList);
        %         [~,idx] = max(numPixels);
        v1 = sum(subimg,1);
        v2 = (sum(subimg,2))';
        x1 = find(v1>0,1,'first');
        x2 = find(v1>0,1,'last');
        y1 = find(v2>0,1,'first');
        y2 = find(v2>0,1,'last');
        % x y w h
        if(max(v1) <1 || max(v2)<1)
            subimg = subimg2;
        else
            subimg = imcrop(subimg,[x1 y1 (x2-x1-1) (y2-y1-1)]);
        end
        
        numResult = numResult +1;
        resulttemp{numResult}= subimg;
    end
    
end
% result = resulttemp;


highO = zeros(1,length(resulttemp));
for i=1:length(resulttemp)
    highO(1,i) = size(resulttemp{i},1);
end
meanValue = mean(highO);
num = 0;
for i=1:length(resulttemp)
    if (highO(1,i) > (meanValue -10)) && (highO(1,i) < (meanValue +10))
        temp1 = resulttemp{i};
        w = size(temp1,2);
        if(w<16)
            temp1= imresize(temp1, [64 NaN]);
            p1=floor((64-w)/2);
            p2=64-w-p1;
            temp1 = padarray(temp1, [0 p1], 0, 'pre');
            if p2>0
                temp1 = padarray(temp1, [0 p2], 0, 'post');
            end
        end
        tempim = imresize(temp1,[64 64]);
        tempim = bwareaopen(tempim, 150);
        msk=[0 0 0 0 ;
            0 1 1 0 ;
            0 1 1 0 ;
            0 0 0 0 ];
        tempim=conv2(double(tempim),double(msk));
        num = num +1;
        result{num} = logical(tempim);
        
    end
end

if IS_DEBUG
    numCharacter = length(result)
    col = ceil(sqrt(numCharacter));
    row = ceil(numCharacter/col);
    figure(2);
    for i=1:numCharacter
        subplot(row, col, i);
        imshow(result{i});
    end
    
end

end

